home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / stackptr.arc / STACKPTR.PAS < prev   
Pascal/Delphi Source File  |  1991-02-12  |  4KB  |  104 lines

  1. UNIT StackPtr;
  2.  
  3.  
  4.                               INTERFACE
  5.  
  6.    TYPE  ElementType = RECORD (* Type of the elements on the stack *)
  7.                            PlateCode: PACKED ARRAY [1..6] OF Char;
  8.                            MoveCount: Integer
  9.                        END;  (* ElementType *)
  10.  
  11.          StackType = ^NodeType;
  12.          NodeType = RECORD
  13.                        Info: ElementType;
  14.                        PreviousNode: StackType
  15.                     END;  (* NodeType *)
  16.  
  17.  
  18.    PROCEDURE StackInit (VAR Stack: StackType);
  19.    FUNCTION StackEmpty (Stack: StackType): Boolean;
  20.    PROCEDURE StackPush (VAR Stack: StackType; NewElem: ElementType);
  21.    PROCEDURE StackPop (VAR Stack: StackType; VAR TopElem: ElementType);
  22.    PROCEDURE StackPeek (Stack: StackType; VAR TopElem: ElementType);
  23.    PROCEDURE StackClear (VAR Stack: StackType);
  24.  
  25.  
  26.                                    IMPLEMENTATION
  27.  
  28.  
  29.  
  30.    PROCEDURE StackInit (VAR Stack: StackType);
  31.  
  32.       BEGIN  (* StackInit *)
  33.          Stack := NIL   
  34.       END;   (* StackInit *)
  35.  
  36.  
  37.    FUNCTION StackEmpty (Stack: StackType):Boolean;
  38.  
  39.       BEGIN  (* StackEmpty *)
  40.          StackEmpty := Stack = NIL
  41.       END;   (* StackEmpty *)
  42.  
  43.    PROCEDURE StackPush (VAR Stack: StackType; NewElem: ElementType);
  44.  
  45.       VAR Temp: StackType;  (* used to allocate space for the new stack element *)
  46.  
  47.       BEGIN  (* StackPush *)
  48.          IF StackEmpty (Stack) THEN
  49.             BEGIN  (* this is the first element in the stack *)
  50.                New (Stack);   (* allocate space for the first element *)
  51.                Stack^.Info := NewElem;  (* put the value of the first element onto the stack *)
  52.                Stack^.PreviousNode := NIL  (* indicate that this is the last node of the stack *)
  53.             END    (* this is the first element in the stack *)
  54.                ELSE
  55.                   BEGIN  (* there is at least one element in the stack *)
  56.                      New (Temp);  (* allocate space for the new node *)
  57.                      Temp^.Info := NewElem;  (* put the value of the new element onto the stack *)
  58.                      Temp^.PreviousNode := Stack;  (* link it to the other elements of the stack *)
  59.                      Stack := Temp;  (* make Stack point to the new top of the stack *)
  60.                   END    (* there is at least one element in the stack *)
  61.       END;   (* StackPush *)
  62.  
  63.  
  64.    PROCEDURE StackPop (VAR Stack: StackType; VAR TopElem: ElementType);
  65.  
  66.       VAR Temp: StackType;  (* used to point to the top of the stack *)
  67.  
  68.       BEGIN  (* StackPop *)
  69.          IF NOT StackEmpty (Stack) THEN
  70.             BEGIN   (* there is at least one element on the stack *)
  71.                TopElem := Stack^.Info;   (* return the value of the top element of the stack *)
  72.  
  73.                (* now remove the top element from the stack *)
  74.  
  75.                Temp := Stack;  (* save the pointer to the top of the stack *)
  76.                Stack := Stack^.PreviousNode; (* disconnect this element from the stack *)
  77.                Dispose (Temp);  (* deallocate space used by the top element *)
  78.             END;    (* there is at least one element on the stack *)
  79.       END;   (* StackPop *)
  80.  
  81.  
  82.    PROCEDURE StackPeek (Stack: StackType; VAR TopElem: ElementType);
  83.  
  84.       VAR Temp: StackType;  (* used to point to the top of the stack *)
  85.  
  86.       BEGIN  (* StackPeek *)
  87.          IF NOT StackEmpty (Stack) THEN  (* if the stack is not empty ... *)
  88.                TopElem := Stack^.Info;   (* return the value of the top element of the stack *)
  89.       END;   (* StackPeek *)
  90.  
  91.  
  92.    PROCEDURE StackClear (VAR Stack: StackType);
  93.  
  94.       VAR Dummy: ElementType;  (* used to hold retrieved elements *)
  95.  
  96.       BEGIN  (* StackClear *)
  97.          WHILE NOT StackEmpty (Stack) DO
  98.             StackPop (Stack, Dummy)
  99.       END;   (* StackClear *)
  100.  
  101.  END.  (* of Unit StackPtr *)
  102.  
  103.  
  104.